#
# Combined Code 
#

################################
### Monte Carlo Code for ECM ###
################################

#
#Clear
#
rm(list=ls())

#
#Packages
#
library(lattice)
library(AER)
library(matrixStats)
library(stats)

#
#Lag Operator Function
#
tslag<-function(y,d=1){
  n<-length(y)
  c(rep(NA,d),y)[1:n]
}

#
# Generate X
#
#   rho gives the degree of autocorrelation in x
#   n is the samle size
#

gen.x <- function(n, rho){
  e <- rnorm(n)
  x <- double(n)
  x[1] <- rnorm(1)
  for(i in 2:n){
    x[i] <- rho * x[i-1] + e[i]
  }
  return(x)
}

# 
# Generate Y for an ADL(1,1)
#
#   n is sample size
#   a and r are the short run effects of x and lag x
#   g is the dynamic effect of y
#   x is the weakly exogenous covariate
#

gen.y <- function(n,a,g,r,x){
  e <- rnorm(n)
  y <- double(n)
  y[1] <- rnorm(1)
  for(i in 2:n){
    y[i] <- g*y[i-1] + a*x[i] + r*x[i-1] + e[i]
  }
  return(y)
}

#
# Function to generate the data frame, estimate the model, and save 
# the model and  the model parameters. 
#
# The function takes as inputs:
#   Coefficients
#   Sample Size
#   And the Number of Montecarlo replications
#   
# The function returns a list of the results for each replication,
# and the experimental values (a,r,g,rho, and n).
#

a<- .25
r<- .5
g<- .5

experiment <- function(a=.25, r=.5, g=.5, rho=0, n=1000, nsim=1000){
  #  set.seed(j)
  res <- vector(mode="list", length=nsim)
  for(m in 1:nsim){
    
    # generate x
    x <- gen.x(n=n, rho=rho)
    # generate y
    y <- gen.y(n=n, a=a, g=g, r=r, x=x)
    # generate lags and differences in x
    x.diff <- x - tslag(x)
    y.diff <- y - tslag(y)
    lagx <- tslag(x)
    lagy <- tslag(y)
    
    #Create Data Frame
    simecm <- data.frame(y, x, x.diff, y.diff, lagx, lagy)
    # run ADL model
    adl <- summary(lm(y~ lagy + x + lagx,data =simecm))
    # run restricted ADL model
    adl.r <- summary(lm(y~ lagy,data =simecm))
    # F test for ADL
    aov.adl <- anova(lm(y~ lagy + x + lagx,data =simecm),lm(y~ lagy,data =simecm))
    adl.F <- aov.adl$F[2]
    f.crit <- qf(.95, df1=2, df2=n-4) 
    adl.F.infer <- ifelse((adl.F)>f.crit,1,0)   
    
    #LRM ADL
    
    b.adl <- adl$coef[,1]
    bt.adl <- t(b.adl)
    lrm.adl.est <- ((b.adl[3] + b.adl[4])/(b.adl[2] - 1))
    
    # run error correction model
    ecm <- summary(lm(y.diff ~ lagy + x.diff  + lagx, data = simecm))
    # save model and parameters 
    # Comparing the ADL and ECM Inferences
    ecm.t.lagx <- ecm$coef[,3][4]
    t.crit <- abs(qt(.025,n-4))
    ecm.t.infer <- ifelse(abs(ecm.t.lagx)>t.crit,1,0)
    ecm.adl.x.compare <- ifelse(adl.F.infer == ecm.t.infer,1,0)
    #IV Reg for LRM SE
    inst <- cbind(1,x, lagy, lagx)
    ivr <- summary(ivreg(y ~ y.diff + x + x.diff | x + inst, data = simecm))
    # Bias
    
    true<-c(0,(g-1),a, (a+r), ((a+r)/(g-1)))
    
    b.ec <- cbind(ecm$coef[,1])
    bt.ec <- t(b.ec)
    estses.ec <- cbind(ecm$coef[,2])
    estparms.ec <-matrix(c(b.ec,b.ec[4,]/b.ec[2,]))
    bias.ec <- matrix(estparms.ec - true)
    
    # MSE
    
    par <- c(0,(g-1),a, (a+r))
    mse.ec <- matrix((bt.ec-par)^2)
    
    # Results
    
    res[[m]] <- list(mod = ecm, mod.adl = adl,rmod.adl = adl.r,aov.adl=aov.adl, 
                     adl.F = adl.F, adl.F.infer=adl.F.infer,ecm.t.lagx=ecm.t.lagx,
                     ecm.t.infer=ecm.t.infer,ecm.adl.x.compare=ecm.adl.x.compare,
                     a=a, r=r, g=g, rho=rho, n=n, mod1 = ivr,estses.ec=estses.ec,
                     bias.ec=bias.ec,mse.ec=mse.ec,lrm.adl.est=lrm.adl.est)
  }
  class(res) <- "ecmsim"
  return(res)
}



#
# Run the experiments
#
#   (n) is a list of values for the different sample sizes you want to
#   calculate for each rho.
#
#   (rho) is a list of autoregressive parameters for X
#
#   (g) is the list of autoregressive parameters for y 
#
#   (a) the relationship between contemporaneous x and y
#
#   (r) the relationship between lag x and y
#
#   (eg) combines n and rho into a matrix that defines the dimensions of the
#   experiments and can be used as a reference. 
#
#   (allexp) holds all the results from all of the regressions for the
#   experiments. 
#

n <- c(25,50)
rho <- c(.5)
g <- c(.5)
a <- c(.25)
r <- c(.5)

eg <- expand.grid(n, rho,g,a,r)
allexp <- vector(mode="list", length=nrow(eg))

system.time(
  
  for(v in 1:nrow(eg)){
    allexp[[v]] <- experiment(n=eg[v,1], rho=eg[v,2],g=eg[v,3],
                              a=eg[v,4],r=eg[v,5], nsim=10)
  }
  
)
#
# A function to summarize the results of the experiments
#
#   (par) is the vector containing the ture values of the parameters
#
#   (true) 

summary.ecmsim <- function(obj, par=c(0,(g-1),a, (a+r)), digits=3){
  
  
  ##
  ## ECM 
  ##
  
  ## GECM
  
  b <- sapply(obj, function(x)x$mod$coef[,1])
  bt <- t(b[1:4,])
  estparms <- cbind(t(b[1:4,]), b[4,]/-b[2,]) # 4 coefficients and LRM
  estses <- t(sapply(obj, function(x)x$mod$coef[,2])) # 4 standard errors for coefficients
  
  # GECM LRM
  
  lrm.ecm <- b[4,]/-b[2,]
  
  ## Bewley 
  
  b2 <- sapply(obj, function(x)x$mod1$coef[,1])
  bt2 <- t(b2[1:4,])
  estparmsivr <- cbind(t(b2[1:4,])) # 4 coefficients and LRM
  
  # Bewley LRM
  
  estlrm <- cbind(b2[3,]) # 4 coefficients and LRM
  
  # SE for LRM
  
  estsesivr <- t(sapply(obj, function(x)x$mod1$coef[,2]))
  lrmse <- cbind(estsesivr[,3])
  
  ##
  ## Reported Values
  ##
  
  #
  # t statistics
  #
  
  # LAG Y and LAG X from GECM
  
  t.na2 <- estparms[,2]/estses[,2] # t-statistic for error correction coefficients
  t.na4 <- estparms[,4]/estses[,4] # t-statistic for lagX
  
  # LRM Bewley
  
  t.LRM <- estparmsivr[,3]/estsesivr[,3]
  
  # Bias EC
  
  bias.sum <- sapply(obj, function(x)x$bias.ec)
  bias <- matrix(rowMeans(bias.sum))
  
  # MSE EC
  
  mse.sum <- sapply(obj, function(x)x$mse.ec)
  mse <- matrix(rowMeans(mse.sum))
  
  # Rejection Rates EC
  
  rejrateEC <- mean(ifelse(t.na2<(-1.645), 1, 0))
  rejrateLAGX <- mean(ifelse(abs(t.na4)>1.96,1,0))
  rejrateLRM <- mean(ifelse(abs(t.LRM)>1.96,1,0))
  
  cis <- estparms[,4] + cbind(-1.96*estses[,4], 1.96*estses[,4])
  covg <- mean(cis[,1] < par[4] & par[4] < cis[,2])
  estLRM <- estlrm
  LRMse <- lrmse
  
  ##
  ## ADL
  ##
  
  ## Parameters and Standard Errors
  
  b.adl <- sapply(obj, function(x)x$mod.adl$coef[,1]) # 4 coefficients
  estparms.adl <- cbind(t(b.adl[1:4,]))
  estses.adl <- t(sapply(obj, function(x)x$mod.adl$coef[,2])) # 4 standard errors for coefficients
  
  ## T Statistics
  
  t.LAGY.adl <- estparms.adl[,2]/estses.adl[,2] # t-statistic for lag y adl
  t.x.adl    <- estparms.adl[,3]/estses.adl[,3] # t-statistic for x adl
  t.LAGX.adl <- estparms.adl[,4]/estses.adl[,4] # t-statistic for lag x adl
  
  ## LRM ADL
  
  lrm.adl <- cbind((b.adl[3,] + b.adl[4,]) / (1 - b.adl[2,]))
  
  # Rejection rates for ADL
  
  rejrateLAGY.adl <- mean(ifelse(abs(t.LAGY.adl)>(1.96), 1, 0))
  rejrateX.adl <- mean(ifelse(abs(t.x.adl)>1.96,1,0))
  rejrateLAGX.adl <- mean(ifelse(abs(t.LAGX.adl)>1.96,1,0))
  
  ##
  ## Comparisons
  ##
  
  #
  # F test X and LagX in ADL vs. t test for LagX in ECM
  #
  
  # Note: A lot of this work is done in the experiment above.
  
  F.X.adl <- mean(sapply(obj,function(x)x$adl.F))
  
  f.x.adl <- sapply(obj,function(x)x$adl.F)
  samerate <- mean(sapply(obj, function(x)x$ecm.adl.x.compare))
  
  ft.bind <- cbind(t.na4,F.X.adl)
  adlVecm <- matrix(colMeans(ft.bind), ncol=1)
  
  
  # Compare LRMS
  
  LRMS <- cbind(lrm.adl,lrm.ecm,estlrm)
  colnames(LRMS) <- c("ADL","ECM","BEWLEY")
  
  # Organize output
  
  rownames(bias) <- c("Constant", "Error Correction Coefficient", 
                      "Contemporaneous Effect of X", "Lag X Effect", "LRM")
  rownames(mse) <- c("Constant", "Error Correction Coefficient", 
                     "Contemporaneous Effect of X", "Lag X Effect")
  colnames(mse) <- "MSE"
  colnames(bias) <- "Average Bias"
  cat("MSE:\n")
  print(round(mse, digits))
  cat("\nBias:\n")
  print(round(bias, digits))
  cat("\nAggregate MSE: ", round(mean(mse), digits), "\n")
  cat("Rejection Rate EC: ", round(rejrateEC, digits), "\n")
  cat("Rejection Rate LAGX: ", round(rejrateLAGX, digits), "\n")
  cat("Rejection Rate LRM: ", round(rejrateLRM, digits), "\n")
  cat("Coverage Rate: ", round(covg, digits), "\n")
  cat("LRM: ", round(mean(estLRM),digits), "\n")
  cat("LRMse: ", round(mean(LRMse),digits), "\n")
  
  
  rownames(adlVecm) <- c("t.lagX.ECM","f.XlagX.adl")
  colnames(adlVecm) <- "Test Statistics"
  
  invisible(list(bias=bias, agmse=mean(mse), mse=mse, rejrateEC=rejrateEC,
                 rejrateLAGX=rejrateLAGX,rejrateLRM=rejrateLRM, 
                 coverage=covg, tstat.ecc.ecm=t.na2,tstat.lagx.ecm = t.na4,
                 f.x.adl=f.x.adl, adlVecm=adlVecm,samerate=samerate,
                 LRMS=LRMS))
}


#
# Returns Elements of summary.ecmsim and t-stats
#

lapply(allexp,summary)


#
# Returns only the elements of sumary.ecmsim
#

for(i in 1:length(allexp)){
  summary(allexp[[i]])
}

#
# Look at individual elements of Allexp
#

# Look through 1-72 (9 rhos and 8 sample sizes)

s <- summary(allexp[[1]])
s

##
## Tables Showing Bias, Rejection, Coverage, and Inference
##

#
# Look at the Bias
#


bias <- round(sapply(allexp, function(x)summary(x)$bias),5)
bias.dat <- cbind(eg, t(bias))
colnames(bias.dat) <- c("T","RHO","G","A","R","CONSTANT","ECC","X","LAGX","LRM")
bias.dat

#
# Look at the MSE
#

mse <- round(sapply(allexp, function(x)summary(x)$mse),5)
mse.dat <- cbind(eg, t(mse))
colnames(mse.dat) <- c("T","RHO","G","A","R","CONSTANT","ECC","X","LAGX","LRM")
mse.dat

#
# Rejection Rates EC
#

rejectionEC.rate <- round(sapply(allexp, function(x)summary(x)$rejrateEC),5)
rejectionEC.dat <- cbind(eg,rejectionEC.rate)
rejectionEC.dat

#
# Rejection Rates LAG X
#

rejectionLAGX.rate <- round(sapply(allexp, function(x)summary(x)$rejrateLAGX),5)
rejectionLAGX.dat <- cbind(eg,rejectionLAGX.rate)
rejectionLAGX.dat

#
# Rejection Rates LRM
#

rejectionLRM.rate <- round(sapply(allexp, function(x)summary(x)$rejrateLRM),5)
rejectionLRM.dat <- cbind(eg,rejectionLRM.rate)
rejectionLRM.dat


#
# Coverage Rates
#

coverage.rate <- round(sapply(allexp, function(x)summary(x)$coverage),5)
coverage.dat <- cbind(eg,coverage.rate)
coverage.dat

#
# Matrix of Rates
#

output.mat <- cbind(eg,rejectionEC.rate,rejectionLAGX.rate,rejectionLRM.rate,coverage.rate)
colnames(output.mat) <- c("T","RHO","g","a","r","RR.ECM","RR.LAGX","RR.LRM","Cov.LAGX")
output.mat

#
# Comparing ECM and ADL
#

rate <- round(sapply(allexp, function(x)summary(x)$samerate),1)
comparison <- round(sapply(allexp, function(x)summary(x)$adlVecm),2) 
comparison.dat <- cbind(eg,t(comparison),rate)
colnames(comparison.dat) <-c("T","RHO","g","a","r","t.lagX.ecm","F.XlagX.adl","SameInferenceTandF?")
comparison.dat


sec2.1.dat <- cbind(eg[1:3],rate,bias.dat[,10],rejectionLRM.rate)
sec2.1.dat



##
## Distributions of Test Statisitics 
##

#
# t-Statistics
#

# Pull t-Statistics out of Allexp

tstats.ecc <- sapply(allexp, function(x)summary(x)$tstat.ecc.ecm)
tstats.lagx <- sapply(allexp, function(x)summary(x)$tstat.lagx.ecm)
fstats.xlagx.adl <- sapply(allexp, function(x)summary(x)$f.x.adl)

# Calculate the Mean and Standard Deviations of the t-statistics
avg.tstats.ecc  <- colMeans(tstats.ecc)
sd.tstats.ecc  <- colSds(tstats.ecc)

avg.tstats.lagx <- colMeans(tstats.lagx)
sd.tstats.lagx <- colSds(tstats.lagx)

avg.fstats <- colMeans(fstats.xlagx.adl)
sd.fstats <- colSds(fstats.xlagx.adl)

# Calculate interesting quantiles 

t.stats.ecc.quantiles <- sapply(allexp, function(x)quantile(summary(x)$tstat.ecc.ecm, c(.025,.25,.5,.75,.975)))
t.stats.lagx.quantiles <- sapply(allexp, function(x)quantile(summary(x)$tstat.lagx.ecm, c(.025,.25,.5,.75,.975)))
f.stats.xlagx.quantiles <- sapply(allexp, function(x)quantile(summary(x)$f.x.adl, c(.025,.25,.5,.75,.975)))

# Bind values into a matrix

tstats.ecc.dat <- cbind(eg,avg.tstats.ecc,sd.tstats.ecc,t(t.stats.ecc.quantiles))
tstats.ecc.dat

tstats.lagx.dat <- cbind(eg,avg.tstats.lagx,sd.tstats.lagx,t(t.stats.lagx.quantiles))
tstats.lagx.dat

fstats.dat <- cbind(eg,avg.fstats,sd.fstats,t(f.stats.xlagx.quantiles))
fstats.dat

# Creates a dataset containing the tstats with corresponding n and rho

newdata.ecc <- data.frame(
  ststats = c(tstats.ecc), 
  n = rep(eg[,1], each=nrow(tstats.ecc)), 
  rho = rep(eg[,2], each=nrow(tstats.ecc))
)

newdata.lagx <- data.frame(
  ststats = c(tstats.lagx), 
  n = rep(eg[,1], each=nrow(tstats.lagx)), 
  rho = rep(eg[,2], each=nrow(tstats.lagx))
)

newdata.fstats <- data.frame(
  ststats = c(fstats.xlagx.adl), 
  n = rep(eg[,1], each=nrow(fstats.xlagx.adl)), 
  rho = rep(eg[,2], each=nrow(fstats.xlagx.adl))
)

#
# Plots t stats of the k experiments over rho
#


densityplot( ~ ststats | rho, groups=n, data=newdata.ecc)

#
# Plots the t stats for a given n over rho
#

densityplot( ~ ststats | n, groups=rho, data=newdata[which(newdata$n == 25), ])
densityplot( ~ ststats | n, groups=rho, data=newdata[which(newdata$n == 50), ])
densityplot( ~ ststats | n, groups=rho, data=newdata[which(newdata$n == 75), ])
densityplot( ~ ststats | n, groups=rho, data=newdata[which(newdata$n == 100), ])
densityplot( ~ ststats | n, groups=rho, data=newdata[which(newdata$n == 250), ])
densityplot( ~ ststats | n, groups=rho, data=newdata[which(newdata$n == 500), ])
densityplot( ~ ststats | n, groups=rho, data=newdata[which(newdata$n == 750), ])
densityplot( ~ ststats | n, groups=rho, data=newdata[which(newdata$n == 1000), ])

##############################
### Monte Carlo Code for d ###
##############################

#
# Clear
#
rm(list=ls())

#
# Packages
#

library(arfima)
library(matrixStats)
library(lattice)

#
# Create Function
#

phi <- .2
theta <- .21
d <- .2
i <- 0
x <- 100
nsim <-10

experiment <- function(phi, theta, d, i, x,nsim)
{
  res <- vector(mode="list", length=nsim)
  for(m in 1:nsim){
    # Simulate
    ts <- arfima.sim(x,model = list(phi = phi, 
                                    theta=theta, 
                                    dfrac=d, 
                                    dint =i))
    # Fit and Summarize
    arfm <- summary(arfima(ts,order = c(1, 0, 0),dmean=FALSE))
    arfm2 <- arfm[5]
    arfm3 <- arfm2$coef[[1]]
    
    #
    res[[m]] <- c(mod = arfm2,d)
  }
  class(res) <- "arfsim"
  return(res)
}

#
# Declare values
#

phi <- c(.6)
theta <- c(0)
d <- c(.3)
i <- c(0)
x <- c(50,100,250)
nsim <- 100

eg <- expand.grid(phi,theta,d,i,x)
eg  
colnames(eg) <-c("phi","theta","d","i","x")

allexp <- vector(mode="list", length=nrow(eg))

#
# Run Experiments
#


for(v in 1:nrow(eg)){
  allexp[[v]] <- experiment(phi = eg[v,1], 
                            theta=eg[v,2], 
                            d=eg[v,3], 
                            i =eg[v,4],
                            x =eg[v,5],
                            nsim=nsim)
}

#
# Summarize Results
#

# Output in a matrix (eg,sim)

output<- as.matrix(allexp)
output.2 <- do.call(rbind,allexp)
output.3 <- apply(output.2,c(1,2),function(x)x[[1]]$mod.coef[1])
output.4 <- matrix(lapply(output.3,function(x)x[[1]]),nrow=nrow(eg),ncol=nsim)

# Matrix of D estimates
output.5 <- matrix(as.numeric(lapply(output.4,function(x)x[3])),nrow=nrow(eg),ncol=nsim)
rownames(output.5) <- (x)
colnames(output.5) <- (1:nsim)

# Matrix of D Standard Errors
output.6 <- matrix(as.numeric(lapply(output.4,function(x)x[7])),nrow=nrow(eg),ncol=nsim)
rownames(output.6) <- (x)
colnames(output.6) <- (1:nsim)

# Matrix of D
output.d <- apply(output.2,c(1,2),function(x)x[[1]][[2]])
rownames(output.d) <- (x)
colnames(output.d) <- (1:nsim)

# Rows of D Estimate Matrix

output.4[[1]]

d.1 <- output.5[1,]                  
d.2 <- output.5[2,]  
d.3 <- output.5[3,]


e.1 <- output.6[1,]                  
e.2 <- output.6[2,]  
e.3 <- output.6[3,]

#Average Estimates
rowMeans(output.5)

#Average CIs

#50

d.low <- c(d.1 - 2.009*(e.1))
d.high <- c(d.1 + 2.009*(e.1))
d.is.50 <- cbind(d.low,d.1,d.high)
d.is.50
l.1 <- colMeans(d.is.50)

#100

d.low <- c(d.2 - 1.984*(e.2))
d.high <- c(d.2 + 1.984*(e.2))
d.is.100 <- cbind(d.low,d.2,d.high)
d.is.100
l.2 <- colMeans(d.is.100)

#250

d.low <- c(d.3 - 1.96*(e.3))
d.high <- c(d.3 + 1.96*(e.3))
d.is.250 <- cbind(d.low,d.3,d.high)
d.is.250
l.3 <- colMeans(d.is.250)

t.o <- cbind(l.1,l.2,l.3)
table.out <- t(t.o)
rownames(table.out) <- (x)
colnames(table.out) <- c("d.low","d.est","d.high")
table.out

##
## Summary
##

# Average Estimated d for each sample size with average CIs

table.out

# Plots of Distributions

plot (density(d.1), col="red",ylim=c(0,3.5),xlim=c(-1,1)) 
lines (density(d.2), col="green") 
lines (density(d.3), col="blue")
lines (density(d.4), col="orange")
lines (density(d.5), col="black")
abline(v=.3)

